package com.cskaoyan.service;

import com.cskaoyan.bean.Issue;
import com.cskaoyan.bean.lzp.IssueData;
import com.cskaoyan.bean.params.BaseParam;
import com.cskaoyan.mapper.IssueMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * \* Description:
 * \* @author: Lizhanpeng
 * \* Date: 2022/5/6
 * \
 */
@Service
public class IssueServiceImpl implements IssueService {

    @Autowired
    IssueMapper issueMapper;

    @Override
    public IssueData query(BaseParam param, String question) {
        Integer page = param.getPage();//页码
        Integer limit = param.getLimit();//数据量
        String sort = param.getSort();//排序列
        String order = param.getOrder();//desc\asc
        PageHelper.startPage(page,limit);
        List<Issue> list =issueMapper.select(sort,order,question);

        PageInfo<Issue> pageInfo = new PageInfo<>(list);
        long total = pageInfo.getTotal();
        int pages = pageInfo.getPages();
        return IssueData.data(total,pages,limit,page,list);
    }

    @Override
    public Issue create(String question, String answer) {
        Issue issue = new Issue();
        issue.setQuestion(question);
        issue.setAnswer(answer);
        issue.setAddTime(new Date());
        issue.setUpdateTime(new Date());
        issue.setDeleted(false);
        int i = issueMapper.insert(issue);
        int maxId= issueMapper.selectMaxId();
        Issue issue1 = issueMapper.selectByPrimaryKey(maxId);
        return issue1;
    }

    @Override
    public int delete(Issue issue) {
        int i = issueMapper.deleteByPrimaryKey(issue.getId());
        return i;
    }

    @Override
    public Issue update(Issue issue) {
        issue.setUpdateTime(new Date());
        issueMapper.updateByPrimaryKeySelective(issue);
        Issue issue1 = issueMapper.selectByPrimaryKey(issue.getId());
        return issue1;
    }
}
